'use strict';

const Controller = require('egg').Controller;
const _ = require('lodash');

class OrderController extends Controller {

  async getOrderSubscriptionList() {
    const ctx = this.ctx;
    const query = {};
    const [ option ] = ctx.getPageListOption()
    let list = await this.ctx.model.OrderSubscription.find(
      query,
      [],
      option
    ).lean();

    if (!list || list.length === 0) {
      ctx.body = {
        success: true,
        code: 1,
        list: [],
      }
      return;
    }

    const fullList = await Promise.all(
      list.map(async (item) => {
        const queryUser = { _id: item.user_id };
        const user = await ctx.model.User.findOne(queryUser).lean();
        if (user) {
          item.userName = _.get(user, 'user_info.nickName', '') || _.get(user, 'name', '患者');
          item.amount 
        }
        return item;
      })
    );
    
    ctx.body = {
      success: true,
      code: 0,
      list: fullList,
    }
  }

  async list() {
    const ctx = this.ctx;
    const query = {};
    const [ option ] = ctx.getPageListOption()

    let list = await this.ctx.model.Order.find(
      query,
      [],
      option
    ).lean();

    if (!list || list.length === 0) {
      ctx.body = {
        success: true,
        code: 1,
        list: [],
      }
      return;
    }

    const fullList = await Promise.all(
      list.map(async (item) => {
        const queryUser = { _id: item.user_id };
        const user = await ctx.model.User.findOne(queryUser).lean();
        if (user) {
          item.userName = _.get(user, 'user_info.nickName', '') || _.get(user, 'name', '患者');
          item.amount 
        }
        return item;
      })
    );

    ctx.body = {
      success: true,
      code: 0,
      list: fullList,
    }
  }
}

module.exports = OrderController;